package com.yotoodesign.dianba.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.yotoodesign.dianba.R;
import com.yotoodesign.dianba.bean.SongList;
import com.yotoodesign.dianba.bean.SongListGroupArray;

public class SongListAdapter extends BaseExpandableListAdapter {
	private SongListGroupArray songListGroupArray;
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	
	public SongListAdapter(Context context,SongListGroupArray songListGroupArray){
		this.mContext = context;
		this.songListGroupArray = songListGroupArray;
		mLayoutInflater = LayoutInflater.from(mContext);
	}
	
	public SongListGroupArray getSongListGroupArray() {
		return songListGroupArray;
	}

	public void setSongListGroupArray(SongListGroupArray songListGroupArray) {
		this.songListGroupArray = songListGroupArray;
	}
	
	@Override
	public int getGroupCount() {
		return songListGroupArray.getSongListGroup().size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return songListGroupArray.getGroup(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return songListGroupArray.getGroup(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return songListGroupArray.getSongList(groupPosition, childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if(convertView==null){
			convertView = mLayoutInflater.inflate(R.layout.song_group_item, null);
		}
		
		TextView song_group_name = (TextView)convertView.findViewById(R.id.song_group_name);
		song_group_name.setText(songListGroupArray.getGroupName(groupPosition));
		
		TextView song_group_num = (TextView)convertView.findViewById(R.id.song_group_num);
		song_group_num.setText(songListGroupArray.getGroup(groupPosition).size()+"条");
		
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if(convertView==null){
			convertView = mLayoutInflater.inflate(R.layout.song_group_item_child, null);
		}
		SongList songList = songListGroupArray.getSongList(groupPosition, childPosition);
		
		TextView tvListName = (TextView)convertView.findViewById(R.id.tvListName);
		tvListName.setText(songList.getName());
		
		TextView tvListDesc = (TextView)convertView.findViewById(R.id.tvListDesc);
		tvListDesc.setText(songList.getDesc());
		
		TextView tvKey = (TextView)convertView.findViewById(R.id.tvKey);
		tvKey.setText(songList.getId());
		
		
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
